package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneFuncProdInstService;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.*;
import com.iwhalecloud.bss.kite.manager.inst.graph.IInstRelGraph;
import com.iwhalecloud.bss.kite.manager.inst.graph.impl.InstVertex;
import com.iwhalecloud.bss.kite.manager.scene.util.InstUtils;
import com.iwhalecloud.bss.kite.manager.scene.util.SceneInstUtils;
import com.iwhalecloud.bss.kite.manager.transaction.TransactionContext;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 漫游国家属性处理器
 */
public class RoamingCountryAttrHandler extends AbstractAttrHandler<KiteProdInstFunc, CustAttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void after(KiteAttr attr, KiteProdInstFunc inst, CustAttrData data) {
        super.after(attr, inst, data);
        String prodInstId = inst.getAccProdInstId();
        if(!KiteStringUtils.isEmpty(prodInstId)){
            KiteProdInst prodInst = querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(),inst.getInstId());
            if(KiteObjectUtils.isEmpty(prodInst)){
                return;
            }
            List<KiteProdInstFunc> funcs = listProdInstFuncs(data.getSceneInstId());
            if(KiteObjectUtils.isEmpty(funcs)){
                return;
            }
            funcs = funcs.stream().filter(func -> KiteStringUtils.equalsAny(ProductCache.get(func.getSpecId()).getProdNbr(), "300000151", LocalKeyConsts.FUNC_PROD_ID_YF5GTX)).collect(Collectors.toList());
            if (!KiteListUtils.isEmpty(funcs)) {
                funcs = funcs.stream().filter(t -> !ActionType.D.equals(t.getActionType()))
                    .collect(Collectors.toList());
                for (KiteProdInstFunc funcProdInst : funcs) {
                    KiteAttr wayAttr = funcProdInst.getAttr("200003690"); // 漫游区域
                    if (Objects.nonNull(wayAttr)) {
                        // 漫游区域是省内、国内的话，不展示漫游国家
                        if (KiteStringUtils.equalsAny(wayAttr.getValue(), "1_0000223", "1_0000224")) {
                            attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                            break;
                        } else {
                            attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                            if ("1_0000225".equals(wayAttr.getValue()) || "100006425".equals(wayAttr.getValue())) {
                                attr.setIsNull(KeyConsts.IFTRUE_F);
                                break;
                            }
                            else {
                                attr.setIsNull(KeyConsts.IFTRUE_T);
                            }
                        }
                    }
                }
            }
        }
    }

    public List<KiteProdInstFunc> listProdInstFuncs(String sceneInstId) {
        List<InstVertex> prodInstFuncVertexs = new ArrayList<>();
        KiteSceneInst sceneInst = TransactionContext.getContainer().getSceneInst(sceneInstId);
        String custId = SceneInstUtils.getCustId(sceneInst);
        IInstRelGraph sceneInstRelGraph = SceneInstUtils.getSceneInstRelGraph(sceneInst);
        List<InstVertex> instVertexs = sceneInstRelGraph.listVertexs();
        Iterator<InstVertex> var8 = instVertexs.iterator();

        while (var8.hasNext()) {
            InstVertex instVertex = var8.next();
            if (!Objects.equals(ActionType.X, instVertex.getActionType())
                && Objects.equals(InstType.PROD_INST_FUNC, instVertex.getInstType())) {
                prodInstFuncVertexs.add(instVertex);
            }
        }
        return InstUtils.transformInsts(custId, prodInstFuncVertexs);
    }
}
